package com.view.my_view;

import com.badibadi.mytools.MyProgressBar;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.AbsListView.OnScrollListener;;
public class XGridView extends GridView implements OnScrollListener{
	
	OnXGridViewListener lOnScrollBottomListener;
	Scroller mScroller;
	View iv;
	/**第一页屏幕中的View的数量*/
	int diyipingmushuliang=-1;
	
	boolean isBottom=false;
	public XGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public XGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public XGridView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context context) {
		// TODO Auto-generated method stub
		  mScroller = new Scroller(context, new DecelerateInterpolator());
		  super.setOnScrollListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
//		
		//第一屏幕的数量
		diyipingmushuliang=visibleItemCount-firstVisibleItem;
		View id=view.getChildAt(0);
		iv=id;
		if((firstVisibleItem+visibleItemCount)==totalItemCount)
		{
			setToTheBottom();
		}
		
		
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		   //2.按下之后为1
		if(scrollState==0 && isBottom==true)
		{
			LoadMore();
		}
	}
//	public void getImageView(CallBack callBack)
//	{
//		callBack.getImg(iv);
//	}
//	public interface CallBack
//	{
//		public void getImg(View iv);
//	}
//	
//	
	public void setXGridViewListener(OnXGridViewListener l)
	{
		lOnScrollBottomListener=l;
		
	}
	public void LoadMore()
	{
		if(lOnScrollBottomListener!=null && isBottom==true)
		{
			MyProgressBar myProgressBar=new MyProgressBar();
			myProgressBar.ProgressBarDialog(getContext(),2000);
			lOnScrollBottomListener.OnLoadMore();
//			new AlertDialog.Builder(getContext()).setView(dialog)
			isBottom=false;
		}
	}
	
	public void setToTheBottom()
	{
		isBottom=true;
	}
	
    public interface OnXGridViewListener
    {
    	void OnLoadMore();
    }
	public boolean isBottom() {
		return isBottom;
	}
    /**设置到底部 */
	public void setBottom(boolean isBottom) {
		this.isBottom = isBottom;
	}
	
//	public void getLLL(CallBack c)
//	{
//		c.getSy(isBottom);
//	}
//   interface CallBack
//    {
//    	void getSy(boolean l);
//    }
}
